/**
 * Author@ Cheng Feitian
 * Date: 2021-4-2
 * Description: 赋值运算符重载
 */

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;
const int maxn = 1024;

class Person
{
public:
    int *m_Age;

    Person(int age)
    {
        m_Age = new int(age);
    }

    ~Person()
    {
        if (this->m_Age != NULL)
        {
            delete this->m_Age;
            this->m_Age = NULL;
        }
    }

    //重载 赋值运算符
    Person & operator=(Person & p){
        //应该先判断是否有属性在堆区，如果有先释放干净，然后再深拷贝
        if (m_Age != NULL)
        {
            delete m_Age;
            m_Age = NULL;
        }
        
        m_Age = new int(*(p.m_Age));
        return *this;
    }
};

void test01()
{
    Person p1(18);
    Person p2(20);
    Person p3(30);

    // p2 = p1;

    // p1.operator=(p2);
    p3 = p2 = p1;

    cout << "p1的年龄为：" << *p1.m_Age << endl;
    cout << "p2的年龄为：" << *p2.m_Age << endl;
    cout << "p3的年龄为：" << *p3.m_Age << endl;
}

int main()
{
    test01();
    return 0;
}
